//刷新数据
/*$.fn.refreshTable = function(url,nowPage){
    refresh(url,nowPage);
};*/



/*
function refresh(url,nowPage){
    if (nowPage !== 0)
        $("#search-form [name=nowPage]").val(nowPage);
    $.ajax({
        type: "POST",
        url: url,
        data: $("#search-form").serialize(),
        success: function (result) {
            $.each(result.pageView.page.items, function (index, content) {
                if (content === null) {
                    result.page.items = [];
                    return false;
                }
                content.created_time = new Date(content.created_time).Format("yyyy-MM-dd HH:mm");// HH:mm:ss
            });

            if(result.pageView.page.items.length === 0 && result.pageView.page.pageNum !==1 ){
                refreshTable(result.pageView.page.pageNum-1);
                return ;
            }

            $(".table-view").html($("#tableTemplate").tmpl(result.pageView));

            $("#page-div").html(pageGenerator(result.pageView));
            initOperation();
        }
    });
}
*/



$.fn.onCreate = function(url){
    $(this).off("click").on("click", function () {
        $.post( url, {}, function (result, textStatus, xhr) {
            $("#commonModal .modal-content").html($("#objTemplate").tmpl(result));
            formValidate();
        });
    });
    //return $(this);
};

$.fn.onEdit = function(url){
    $(this).off("click").on("click", function () {
        $.post(url, {id: $(this).closest("tr").data("id")}, function (result, textStatus, xhr) {
            $("#commonModal .modal-content").html($("#objTemplate").tmpl(result));
            formValidate();
        });
    });
    //return $(this);
};

$.fn.initDelete = function(url){
    $(this).off("click").on("click", function () {
        var id = $(this).closest("tr").data("id");
        $.confirm({
            title: '系统提示',
            content: "确认要删除'<font color='#f00'>" + $(this).closest("tr").data("name") + "</font>'吗？",
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: function () {
                        $.post(url, {id: id}, function (data, textStatus, xhr) {
                            if (data > 0) {
                                alert(data)
                                refreshTable(0);
                                noty_success('删除成功');
                            } else {
                                noty_fail('删除失败');
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                },
            }
        });
    });
    return $(this);
};

$.fn.initDisable = function(url) {

    $(this).off("click").on("click", function () {
        var id = $(this).closest("tr").data("id");
        $.confirm({
            title: '系统提示',
            content: "确认要禁用'<font color='#f00'>" + $(this).closest("tr").data("name") + "</font>'吗？",
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: function () {
                        $.post(url, {id: id}, function (data, textStatus, xhr) {
                            if (data > 0) {
                                refreshTable(0);
                                noty_success('禁用成功');
                            } else {
                                noty_fail('操作失败');
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                },
            }
        });
    });
    return $(this);
};
$.fn.initEnable = function(url) {

    $(this).off("click").on("click", function () {
        var id = $(this).closest("tr").data("id");
        $.confirm({
            title: '系统提示',
            content: "确认要启用'<font color='#f00'>" + $(this).closest("tr").data("name") + "</font>'吗？",
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: function () {
                        $.post(url, {id: id}, function (data, textStatus, xhr) {
                            if (data > 0) {
                                refreshTable(0);
                                noty_success('启用成功');
                            } else {
                                noty_fail('操作失败');
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                },
            }
        });
    });
    return $(this);
};

$.fn.initBatchDelete = function(url) {

    $(this).off("click").on("click", function () {
        var checkbox = $("input[type=checkbox]:checked");
        if (checkbox.length === 0) return;
        var content = "";
        for (var i = 0; i < checkbox.length; i++) {
            content += "'<font color='#3671be'>" + checkbox.eq(i).closest("tr").data("name") + "</font>'&nbsp;";
        }
        $.confirm({
            title: '系统提示',
            content: "确认要删除" + content + ",这<font color='#f00'>" + checkbox.length + "</font>条数据吗？",
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: function () {
                        $.post(url, $(".ids-form").serialize(), function (data, textStatus, xhr) {
                            if (data > 0) {
                                refreshTable(0);
                                noty_success('删除成功');
                            } else {
                                noty_fail('删除失败');
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                },
            }
        });
    });
    return $(this);
};


function pageGenerator(page) {
    if(page.page.totalPage !== 1){

        var headPage = '';
        if(page.page.pageNum === 1){
            headPage = '<li class="disabled"><a href="javascript:void(0);">上一页</a></li>';
        }else{
            headPage = '<li><a href="javascript:refreshTable(${page.page.pageNum-1||1})"><span>上一页</span></a>';
        }

        var middlePage = '';
        $.each(page.showPages, function (i, v) {
            if(v === page.page.pageNum){
                middlePage += '<li class="active"><a href="javascript:void(0);">${v}</a></li>';
            }else if(v === 0){
                middlePage += '<li><a>...</a></li>';
            }else{
                middlePage += '<li><a href="javascript:refreshTable(${v||0})"><span>${v||0}</span></a></li>';
            }

        });

        var tailPage = '';

        if(page.page.pageNum === page.page.totalPage){
            tailPage = '<li class="disabled"><a href="javascript:void(0);">下一页</a></li>';
        }else{
            tailPage = '<li><a href="javascript:void(0);" onclick="refreshTable(${page.page.pageNum+1||1})"><span>下一页</span></a>';
        }

        var pageHtml =
            '<nav class="zlzkj_pagination">'
        '<ul class="pagination">'
        headPage+
        middlePage+
        tailPage+
        '</ul>'+
        '</nav>';
        return pageHtml;
    }
}
